package com.johnny.xia.util.date;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期相关处理工具
 * @author johnnyxia
 *
 */
public class DateUtil {
	
	/**
	 * 将字符串转成Date对象
	 * @param str
	 * 			日期字符串
	 * @param pattern
	 * 			日志格式化模板
	 * @return
	 * @throws ParseException
	 */
	public static Date str2Date(String str,String pattern) throws ParseException{
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.parse(str);
	}
	
	/**
	 * 将日期格式化成str类型
	 * @param date
	 * 			目标日期
	 * @param pattern
	 * 			格式化模板
	 * @return
	 */
	public static String date2Str(Date date,String pattern){
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	
	/**
	 * 获取两个日期相差的月份，不足一个月算一个月
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getMonth(Date date1,Date date2){
		date1 = resetDayOfBegin(date1);
		date2 = resetDayOfBegin(date2);
		
		double subDay = (date2.getTime()-date1.getTime())/(24*60*60*1000);
		
		if(subDay < 30){
			return 1;
		}else{
			return BigDecimal.valueOf(subDay/30).setScale(0, BigDecimal.ROUND_UP).intValue();
		}
		
		
		
	}
	
	/**
	 * 将指定日期重置为当天的开始时间
	 * @param date
	 * @return
	 */
	public static Date resetDayOfBegin(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 将指定日期重置为当天的结束时间
	 * @param date
	 * @return
	 */
	public static Date resetDayOfEnd(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}
	
	public static int diffDay(Date start,Date end){
	    int days = (int) ((end.getTime() - start.getTime()) / (1000*3600*24));
        return days;
	}
	
	   /**
     * 获取两个相隔天数
     * endDate > startDate为true,否则false
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getApartDate(Date startDate, Date endDate) {
        Calendar sd = Calendar.getInstance();
        Calendar ed = Calendar.getInstance();
        sd.setTime(startDate);
        int day1 = sd.get(Calendar.DAY_OF_YEAR);
        ed.setTime(endDate);
        int day2 = ed.get(Calendar.DAY_OF_YEAR);
        sd = null;
        ed = null;
        return (day2 - day1) + 1;
    }
	
	
	public static void main(String[] args) throws ParseException{
//      int d =  diffDay(resetDayOfBegin(str2Date("2017-05-14 23:59:59", "yyyy-MM-dd HH:mm:ss")), resetDayOfEnd(str2Date("2017-05-14 00:01:00", "yyyy-MM-dd HH:mm:ss")));
	    
//	    int d = getApartDate(str2Date("2017-05-14 23:59:59", "yyyy-MM-dd HH:mm:ss"), str2Date("2016-05-16 00:01:00", "yyyy-MM-dd HH:mm:ss"));
//      System.out.println(d);
      
      SimpleDateFormat format = new SimpleDateFormat("HHmmssSSS");
      String s = format.format(str2Date("2017-05-14 11:59:59.089", "yyyy-MM-dd HH:mm:ss.SSS"));
      System.out.println(s);
      
      if(s.startsWith("0")){
          System.err.println(Integer.parseInt(s.substring(1)));
      }else{
          System.err.println( Integer.parseInt(s));
      }
    }
	
}
